package threshold;

import java.util.Arrays;

public class FindMedianSortedArrays {

    //将两个有序数组，合并成一个有序数组
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {

        int[] re = new int[nums1.length + nums2.length];
        int i,j,k;
        for(k=0,i=0,j=0;i<nums1.length&&j<nums2.length;k++){
            if(nums1[i]<=nums2[j]){
                re[k] = nums1[i++];
            }else {
                re[k] = nums2[j++];
            }
        }
        while(i<nums1.length){
            re[k++] = nums1[i++];
        }
        while(j<nums2.length){
            re[k++] = nums2[j++];
        }
        System.out.println(Arrays.toString(re));
        double i1;
        if(re.length%2==0){
            i1 =(double) (re[re.length / 2] + re[re.length / 2 - 1])/2;
        }else{
            i1 = (double)re[re.length / 2];
        }
        return i1;
    }

    public static void main(String[] args) {
        FindMedianSortedArrays test = new FindMedianSortedArrays();
        int nums1[] = {1,3};
        int nums2[] = {2};
        System.out.println(test.findMedianSortedArrays(nums1,nums2));
    }
}
